using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Glpt.Common
{
    /// <summary>
    /// 多选框控件
    /// </summary>
    public class CheckedListBoxControl : CheckedListBox
    {
        private DataView _source = null;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public CheckedListBoxControl()
        {
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="source">数据源</param>
        public void DataBind(DataView source)
        {
            _source = source;
            DataDictionary.DataBind(source, this);
        }

        private void ShowFrom(KeyPressEventArgs e)
        {
            FrmCombListDialog frm = new FrmCombListDialog();
            if (_source != null)
                frm.DataBind(_source);
            else
            {
                frm.CheckedListControl.Items.Clear();
                for (int i = 0; i < this.Items.Count; i++)
                {
                    frm.CheckedListControl.Items.Add(this.Items[i]);
                }
            }
            frm.SetCheckListBoxStatus(GetCheckListBoxValues());
            frm.ListControl = this;
            if (e != null)
            {
                frm.KeyCharArgs = e;
                e.Handled = true;
            }
            frm.Location = this.PointToScreen(new Point(-2, -2));
            if (frm.Width < this.Width)
                frm.AddWidth(this.Width - frm.Width);
            frm.Show();
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.KeyPress 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.KeyPressEventArgs。</param>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)13)
            {
                if (_source != null && _source.Count > 5)
                {
                    ShowFrom(e);
                }
            }
            else
                base.OnKeyPress(e);
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseDown 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            ShowFrom(null);
        }
        /// <summary>
        /// 设置选中的CheckListBox列表项
        /// </summary>
        /// <param name="valuesList">以逗号隔开的值列表</param>
        public void SetCheckListBoxStatus(string valuesList)
        {
            SetCheckListBoxStatus(this, valuesList, false);
        }

        /// <summary>
        /// 设置选中的CheckListBox列表项
        /// </summary>
        /// <param name="valuesList">以逗号隔开的值列表</param>
        /// <param name="isadd">是否增加项</param>
        public void SetCheckListBoxStatus(string valuesList, bool isadd)
        {
            SetCheckListBoxStatus(this, valuesList, isadd);
        }

        /// <summary>
        /// 获取选中的CheckListBox列表项的值
        /// </summary>
        /// <returns>选中的值列表</returns>
        public string GetCheckListBoxValues()
        {
            return GetCheckListBoxValues(this);
        }

        /// <summary>
        /// 获取选中的CheckListBox列表项的文本
        /// </summary>
        /// <returns>选中的文本列表</returns>
        public string GetCheckListBoxTexts()
        {
            return GetCheckListBoxTexts(this);
        }

        /// <summary>
        /// 获取选中的CheckListBox列表项的值
        /// </summary>
        /// <param name="chklist">CheckedListBox控件</param>
        /// <returns>选中的值列表</returns>
        public static string GetCheckListBoxValues(CheckedListBox chklist)
        {
            StringBuilder selectbuf = new StringBuilder();
            for (int i = 0; i < chklist.Items.Count; i++)
            {
                if (chklist.GetItemChecked(i))
                {
                    DictElement elem = (DictElement)chklist.Items[i];
                    selectbuf.Append(',');
                    selectbuf.Append(elem.Value);
                }
            }
            if (selectbuf.Length > 0) selectbuf.Remove(0, 1);
            return selectbuf.ToString();
        }
        /// <summary>
        /// 获取选中的CheckListBox列表项的文本
        /// </summary>
        /// <param name="chklist">CheckedListBox控件</param>
        /// <returns>选中的文本列表</returns>
        public static string GetCheckListBoxTexts(CheckedListBox chklist)
        {
            StringBuilder selectbuf = new StringBuilder();
            for (int i = 0; i < chklist.Items.Count; i++)
            {
                if (chklist.GetItemChecked(i))
                {
                    DictElement elem = (DictElement)chklist.Items[i];
                    selectbuf.Append(',');
                    selectbuf.Append(elem.Text);
                }
            }
            if (selectbuf.Length > 0) selectbuf.Remove(0, 1);
            return selectbuf.ToString();
        }
        /// <summary>
        /// 设置选中的CheckListBox列表项
        /// </summary>
        /// <param name="chklist">CheckedListBox控件</param>
        /// <param name="valuesList">以逗号隔开的值列表</param>
        /// <param name="isadd">是否增加项</param>
        public static void SetCheckListBoxStatus(CheckedListBox chklist, string valuesList, bool isadd)
        {
            String[] units = valuesList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < chklist.Items.Count; i++)
            {
                DictElement dict = (DictElement)chklist.Items[i];
                if (!isadd) chklist.SetItemChecked(i, false);
                for (int j = 0; j < units.Length; j++)
                {
                    if (dict.Value.Equals(units[j]) || units[j].Equals("@"))
                    {
                        chklist.SetItemChecked(i, true);
                        chklist.SetSelected(i, true);
                        break;
                    }
                }
            }
        }
    }
}
